{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "13f9e18b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch \n",
    "from torch.utils import data\n",
    "from d2l import torch as d2l\n",
    "\n",
    "true_w = torch.tensor([2, -3.4])\n",
    "true_b = 4.2\n",
    "features, labels = d2l.synthetic_data(true_w, true_b, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a7839cd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[ 0.2025,  1.1288],\n",
       "         [-0.1274,  1.0536],\n",
       "         [ 0.2218,  0.0915],\n",
       "         [-0.4873,  1.4626],\n",
       "         [ 0.1701, -1.5187],\n",
       "         [ 0.6973, -1.3176],\n",
       "         [ 0.1679, -1.8545],\n",
       "         [ 0.3516,  0.2256],\n",
       "         [ 0.9920,  0.5087],\n",
       "         [-0.5597, -1.7754]]),\n",
       " tensor([[-0.4736],\n",
       "         [ 5.6781],\n",
       "         [ 1.7308],\n",
       "         [ 3.5088],\n",
       "         [ 5.4141],\n",
       "         [-3.3462],\n",
       "         [ 1.5317],\n",
       "         [-4.8731],\n",
       "         [ 6.5119],\n",
       "         [ 8.1386]])]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def load_array(data_arrays, batch_size, is_train=True):\n",
    "    \"\"\"构造一个Pytorch数据迭代器\"\"\"\n",
    "    dataset = data.TensorDataset(*data_arrays)\n",
    "    return data.DataLoader(dataset, batch_size, shuffle = is_train)\n",
    "\n",
    "batch_size = 10\n",
    "data_iter = load_array((features, labels), batch_size)\n",
    "iter(data_iter).next() #iter()返回一个迭代对象，其含有next()方法，还可写成next(iter())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9b4b9030",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch import nn #nn是神经网络的缩写\n",
    "\n",
    "net = nn.Sequential(nn.Linear(2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "efd78721",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[0].weight.data.normal_(0, 0.01) #使用正态分布来初始化\n",
    "net[0].bias.data.fill_(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7a97ee93",
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = nn.MSELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f51d60cd",
   "metadata": {},
   "outputs": [],
   "source": [
    " trainer = torch.optim.SGD(net.parameters(), lr=0.03) \n",
    "#拿出网络中所有参数 net.parameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ab1f33a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 14.926668\n",
      "epoch 2, loss 14.688319\n",
      "epoch 3, loss 14.433546\n"
     ]
    }
   ],
   "source": [
    "num_epoches = 3\n",
    "for epoch in range(num_epoches):\n",
    "    for x, y in data_iter:\n",
    "        l = loss(net(x), y)\n",
    "        trainer.zero_grad() #优化器梯度清零\n",
    "        l.backward()\n",
    "        trainer.step() #进行一次模型更新\n",
    "    \n",
    "    l = loss(net(features), labels)\n",
    "    print(f\"epoch {epoch + 1}, loss {l:f}\")\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "201da9b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w的估计误差： tensor([ 1.9749, -3.3021])\n",
      "b的估计误差： tensor([-0.1373])\n"
     ]
    }
   ],
   "source": [
    "w = net[0].weight.data\n",
    "b = net[0].bias.data\n",
    "\n",
    "print('w的估计误差：', true_w - w.reshape(true_w.shape))\n",
    "print('b的估计误差：', true_b - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c4c566a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
